﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ImageOptimizationTest.Optimization;
using Path = System.IO.Path;

namespace ImageOptimizationTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public static readonly DependencyProperty OutputFilePathProperty = DependencyProperty.Register(
            "OutputFilePath", typeof(string), typeof(MainWindow), new PropertyMetadata(default(string)));

        public static readonly DependencyProperty SourceFilePathProperty = DependencyProperty.Register(
            "SourceFilePath", typeof(string), typeof(MainWindow), new PropertyMetadata(default(string)));

        public static readonly DependencyProperty OptimizeParamProperty = DependencyProperty.Register(
            "OptimizeParam", typeof(int), typeof(MainWindow), new PropertyMetadata(90));

        public static readonly DependencyProperty OriginFileSizeProperty = DependencyProperty.Register(
            "OriginFileSize", typeof(double), typeof(MainWindow), new PropertyMetadata(default(double)));

        public static readonly DependencyProperty OutputFileSizeProperty = DependencyProperty.Register(
            "OutputFileSize", typeof(double), typeof(MainWindow), new PropertyMetadata(default(double)));

        public double OutputFileSize
        {
            get { return (double) GetValue(OutputFileSizeProperty); }
            set { SetValue(OutputFileSizeProperty, value); }
        }

        public double OriginFileSize
        {
            get { return (double) GetValue(OriginFileSizeProperty); }
            set { SetValue(OriginFileSizeProperty, value); }
        }

        public int OptimizeParam
        {
            get { return (int) GetValue(OptimizeParamProperty); }
            set { SetValue(OptimizeParamProperty, value); }
        }

        public string SourceFilePath
        {
            get { return (string) GetValue(SourceFilePathProperty); }
            set { SetValue(SourceFilePathProperty, value); }
        }

        public string OutputFilePath
        {
            get { return (string)GetValue(OutputFilePathProperty); }
            set { SetValue(OutputFilePathProperty, value); }
        }

        public MainWindow()
        {
            InitializeComponent();
        }

        private void StartOptimize_OnDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                var file = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
                OriginFileTextBox.Text = file;
                SourceFilePath = file;
                OriginFileSize = (double)new FileInfo(file).Length / 1024 / 1024;
                OriginFileExtensionTextBlock.Text = Path.GetExtension(file);
            }
        }

        private void StartOptimize_OnClick(object sender, RoutedEventArgs e)
        {
            try
            {
                var destDir = TempDirTextBox.Text;
                if (string.IsNullOrWhiteSpace(destDir) || !Directory.Exists(destDir))
                {
                    destDir = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
                        "temp_picture_optimization_test");
                }
                Directory.CreateDirectory(destDir);

                SourceFilePath = OriginFileTextBox.Text;
                var handler = new ImageOptimizationHandler();
                handler.JpgQualityLevel = OptimizeParam;
                OutputFilePath = handler.Optimization(SourceFilePath, destDir, 4096, 4096);
                OutputFileSize = (double)new FileInfo(OutputFilePath).Length / 1024 / 1024;
                OptimizeFileExtensionTextBlock.Text = Path.GetExtension(OutputFilePath);
                OptimizeRateTextBlock.Text = (OutputFileSize / OriginFileSize).ToString("P");
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Error");
            }

        }
    }
}
